﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tanmia.Domain;
using Tanmia.Core;

namespace Tanmia.Controls
{
    /// <summary>
    /// Interaction logic for DIComboBox.xaml
    /// </summary>
    public partial class DIComboBox : UserControl
    {
        public static readonly DependencyProperty DiscriminatedItemProperty = DependencyProperty.Register("DiscriminatedItem", typeof(DiscriminatedItem), typeof(DIComboBox), new PropertyMetadata(new DiscriminatedItem()));

        private DiscriminatedItemDomain DiscriminatedItemObj = new DiscriminatedItemDomain();
        private ItemDomain ItemObj = new ItemDomain();
        private ColorDomain ColorObj = new ColorDomain();
        private PersonDomain PersonObj = new PersonDomain();

        public DiscriminatedItem DiscriminatedItem
        {
            get
            {
                return (DiscriminatedItem)GetValue(DiscriminatedItemProperty);
            }
            set
                {
                if (value != null)
                {
                    SetValue(DiscriminatedItemProperty, value);

                    cmbItem.SelectedItem = value.Item;
                    cmbColor.SelectedItem = value.Color;
                    cmbManufacturer.SelectedItem = value.Manufacturer;
                }
            }
        }

        public DIComboBox()
        {
            InitializeComponent();

            try { cmbItem.ItemsSource = CollectionViewSource.GetDefaultView(ItemObj.GetItemsInDiscriminatedItem()); cmbItem.SelectedIndex = 0; }
            catch { }
        }

        private void cmbItem_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbItem.SelectedItem != null && cmbItem.SelectedItem != Tanmia.Core.Item.SelectItem)
            {
                cmbColor.ItemsSource = CollectionViewSource.GetDefaultView(ColorObj.GetsByItem(cmbItem.SelectedItem as Item));
                cmbColor.SelectedIndex = 0;
            }
        }

        private void cmbColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbColor.SelectedItem != null && cmbColor.SelectedItem != Tanmia.Core.Color.SelectColor)
            {
                cmbManufacturer.ItemsSource = CollectionViewSource.GetDefaultView(PersonObj.GetsByItemColor(cmbItem.SelectedItem as Item, cmbColor.SelectedItem as Tanmia.Core.Color));
                cmbManufacturer.SelectedIndex = 0;
            }
        }

        private void cmbManufacturer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cmbManufacturer.SelectedItem != null)
                DiscriminatedItem = DiscriminatedItemObj.GetDiscriminatedItem(cmbItem.SelectedItem as Item, cmbColor.SelectedItem as Tanmia.Core.Color, cmbManufacturer.SelectedItem as Person);
        }
    }
}
